package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

import tool.GetVertex;
import tool.GetVertexOfEdge;
import tool.Settings;

public class DrawEdge extends JFrame {
	private static final long serialVersionUID = 1L;
	private static int[][] edges = GetVertexOfEdge.getEdges();
	private static int[][] vertexMatrix= GetVertex.getVertex();
	private int rows = vertexMatrix.length;
	int magnification = 0;
	Draw de = new Draw();
	
	DrawEdge(){
		this.add(de);
	}
	
	public static void main(String[] args){
		JFrame frame = new DrawEdge();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screen = kit.getScreenSize();
		int screenWidth = screen.width;
		int screenHeight = screen.height;
		frame.setSize(4 * screenWidth/5, 4 * screenHeight/5);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle(Settings.frameTitle);
		frame.setVisible(true);
	}
	
	public class Draw extends JPanel {

		private static final long serialVersionUID = 1L;
		boolean isShowEdge = true;
		
		public void paintComponent(Graphics g) {

			super.paintComponent(g);
			g.translate(0, this.getHeight());
			g.setColor(Color.black);
			magnification = getMagnification(vertexMatrix);
			
			int mov = 20;
			for(int i = 0; i < edges.length; i++)
				for(int j = 0; j < edges.length; j++){
					if(edges[i][j] == 1){
						int Point1X = vertexMatrix[i][0];
						int Point1Y = vertexMatrix[i][1];
						int Point2X = vertexMatrix[j][0];
						int Point2Y = vertexMatrix[j][1]; 
						g.fillOval(magnification * Point1X + mov, -magnification * Point1Y - mov, 5, 5);
						g.fillOval(magnification * Point2X + mov, -magnification * Point2Y - mov, 5, 5);
						g.drawLine(magnification * Point1X + mov + 2,
								-magnification * Point1Y + 2 - mov, magnification
										* Point2X + 2 + mov, -magnification
										* Point2Y + 2 - mov);
						g.drawString("(" + Point1X + "," + Point1Y + ")", magnification * Point1X - 15 + mov,
								-magnification * Point1Y + 18 - mov);
						g.drawString("P" + i, magnification * Point1X - 5 + mov, -magnification * Point1Y
								- 3 - mov);
					}
				}
		}
		
		public int getMaxX(int[][] vertexMatrix){
			int max = 0;
			for(int i = 0; i < rows; i++){
				if(vertexMatrix[i][0] > max)
					max = vertexMatrix[i][0];
			}
			return max;
		}
		
		public int getMaxY(int[][] vertexMatrix){
			int max = 0;
			for(int i = 0; i < rows; i++){
				if(vertexMatrix[i][1] > max)
					max = vertexMatrix[i][1];
			}
			return max;
		}
		
		public int getMagnification(int[][] vertexMatrix){
			int mag = 0;
			int maxXCoordinate = getMaxX(vertexMatrix);
			int maxYCoordinate = getMaxY(vertexMatrix);
			int magX = (this.getWidth() - 50)/maxXCoordinate;
			int magY = (this.getHeight() - 50)/maxYCoordinate;
			mag = (magX > magY) ? magY : magX;
			return mag;
		}
	}
}
